package com.group01.web.controller;

import com.group01.result.Result;
import com.group01.service.UploadService;
import com.group01.utils.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/21
 */
@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "文件上传接口")
public class UploadController {
    /**
     * 文件上传
     */
    private final UploadService uploadService;

    public UploadController(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * 本地上传
     * @param img 图片
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("本地上传 + Nginx")
    @PostMapping("/upload1")
    public Result<String> LocalUpload1(MultipartFile img) {
        String url = UploadUtils.upload(img);
        return Result.success("上传成功", url);
    }

    /**
     * 云端存储
     * @param img 文件对象
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("云端上传")
    @PostMapping("/upload")
    public Result<String> CloudUpload(@RequestParam("file") MultipartFile img) {
        return uploadService.upload(img);
    }
}
